export default function loadScript(src:any) {
  return new Promise(function (resolve:any, reject:any) {
    const tag = document.getElementsByTagName('script')
    for (let i of tag) {
      if (i.src === src) {
        resolve()
        return
      }
    }
    const script = document.createElement('script')
    script.type = 'text/javascript'
    script.src = src
    script.onerror = reject
    document.body.appendChild(script)
    script.onload = () => {
      resolve()
    }
  })
}

export function LoadBaiduMapScript() {
  //console.log("初始化百度地图脚本...");
  const BMap_URL =
    '//api.map.baidu.com/api?v=3.0&ak=bUU4IBE5Rq2tgFyMvNXlG3GNrwqVdvQG&callback=onBMapCallback'
  return new Promise((resolve) => {
    // 如果已加载直接返回
    if (typeof window.BMap !== 'undefined') {
      resolve(window.BMap)
      return true
    }
    // 百度地图异步加载回调处理
    window.onBMapCallback = function () {
      console.log('百度地图脚本初始化成功...')
      resolve(window.BMap)
    }
    // 插入script脚本
    const scriptNode = document.createElement('script')
    // scriptNode.setAttribute("type", "text/javascript");
    scriptNode.setAttribute('src', BMap_URL)
    document.head.appendChild(scriptNode)
  })
}
